<template>
<div>
<div class="image__placeholder" style="width: 1366px; height: 600px; margin-top: 18px;background-image: url(http://127.0.0.1:8080/ipfs/Qmbeg7UUo4MjXZoxaJLTcmdvszb49LABosCNwJZcGhfapv);backdrop-filter: blur(5px); position: relative; padding:1px;
    box-sizing:border-box;z-index:1;background-size: 1366px 600px;">
  <div class="block" >
    <el-image 
    style="width: 618px; height: 399px;margin-top: 20px; margin-right: 85px;float: right; box-shadow: 0 5px 12px 2 rgba(0, 0, 0, 0.1);z-index:11;"
    src='http://127.0.0.1:8080/ipfs/QmY4zZ3rNpT6HqQMLQnog7N1aXDw7y6y8jyZybgjbwuz82'></el-image>
    <p style="font-size: 6ch;font-weight: bold;color: #383838; padding-top: 50px;">数字资产<br>版权通证交易所</p>
    <p style="color: #524A4E;">在这里，可以通过对版权通证的交易<br><br>来实现对数字资产的出售和购买</p>
  </div>
  <div class="block2" style="margin-top: 50px;">
    <el-button type="primary" style="font-weight: bold;  font-size: larger;" @click="dialogTableVisible = true">出售我的通证</el-button>
    <el-button type="primary" style="margin-left: 70px;  font-weight: bold;font-size: larger; " @click="$router.push('/explore')">想要购买通证</el-button>
  </div>
 <!-- <p style="font-size: 6ch;font-weight: bold;color: #383838; padding-top: 30px;">数字资产<br>版权通证交易所</p> -->
    
</div>

<!-- Form -->

    <el-dialog title="选择要交易的版权通证" :visible.sync="dialogTableVisible">
      <el-table :data="nfts" height="250" style="width: 100%">
        <!-- <el-table-column
          v-for="(item, index) in nfts[0]"
          :key="index"
          :label="item"
        >
          <template slot-scope="scope">
            <span>{{ scope.row[index] }}</span>
          </template>
        </el-table-column> -->

        <el-table-column label="通证名称" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row[3] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="认证时间" width="160">
          <template slot-scope="scope">
            <span>{{scope.row[6]}}</span>
          </template>
        </el-table-column>
        <el-table-column label="最近交易时间" width="160">
          <template slot-scope="scope">
            <span>{{scope.row[7]}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="info" size="small"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="可交易状态" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row[2]"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleClick1(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="date" label="可出售日期" width="180">
        </el-table-column> -->
        <!-- 查看信息 -->
        <!-- <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="info" size="small"
              >查看</el-button
            >
          </template>
        </el-table-column> -->
        <!-- 交易状态开关 -->
      </el-table>
    </el-dialog>
    <!-- Form -->
  
</div>
</template>
<script>
export default {
  data() {
    return {
      statement: "1",
      nfts: [],
      myname: "hello world",
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          test: "aa",
          value: true,
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          test: "aa",
          value: true,
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          test: "aa",
          value: false,
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          test: "aa",
          value: false,
        },
      ],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "120px",
    };
  },

  async mounted() {
    console.log("dispatching getContractInstance");
    this.$store.dispatch("getContractInstance");
    console.log("dispatching getContractInstance1");
    this.$store.dispatch("getContractInstance1");
    if (!this.$store.state.contractInstance) {
      console.log("dispatching getContractInstance");
      await this.$store.dispatch("getContractInstance");
    }

    this.getUserNft();
  },
  computed: {
    statustoint(statement) {
      return parseInt(statement);
    },
  },
  methods: {
    test() {
      console.log(this.nfts);
    },
    handleClick(row) {
      //   console.log(tableData[row].data);
      console.log(row);
      console.log(row.name);
      console.log(row.test);
      //  this.row.name = row.name;
      //  this.row.date = row.date;
      //  console.log(this.row.date);
      //  console.log(this.row.name);
      // this.tableData[row].date = row.date;
      // this.tableData[row].name = row.name;
      // console.log(this.tableData[row].date);
      // console.log(this.tableData[row].name);
      this.$alert(
        '<lable style="font-weight: bold; height: 300px; font-size: medium;" >通证名称：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</lable><lable style="font-size: medium;" >' +
          row[3] +
          '</lable><br><br><lable style="font-weight: bold; height: 300px; font-size: medium;">认证时间：&nbsp;&nbsp;</lable><lable style="font-size: medium;" >' +
          row[6] +
          '</lable><br><br><lable style="font-weight: bold; height: 300px; font-size: medium;">描述内容：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</lable><lable style="font-size: medium;" >' +
          row[5] +
          '</lable><br><br><lable style="font-weight: bold; height: 300px; font-size: medium;">通证类型：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</lable><lable>ICT</lable><br>',
        "版权通证详情",
        {
          dangerouslyUseHTMLString: true,
          confirmButtonText: "确定",
        }
      );
    },
    handleClick1(row) {
      let that = this;
      console.log(row);
      if (!this.$store.state.contractInstance1) {
        console.log("dispatching getContractInstance1");
        this.$store.dispatch("getContractInstance1");
      }
      if (row[2] === true) {
        this.$prompt(
          '<lable style="font-weight: bold; height: 300px; font-size: medium;" >通证名称：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</lable><lable style="font-size: medium;" >' +
            row[3] +
            '</lable><br><br><lable style="font-weight: bold; height: 300px; font-size: medium;">可出售时间：&nbsp;&nbsp;</lable><lable style="font-size: medium;" >' +
            row[6] +
            '</lable><br><br><lable style="font-weight: bold; height: 300px; font-size: medium;">描述：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</lable><lable style="font-size: medium;" >' +
            row[5] +
            '</lable><br><br><lable style="font-weight: bold; height: 300px; font-size: medium;">通证类型：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</lable><lable>ICT</lable><br>',
          "开启交易",
          {
            dangerouslyUseHTMLString: true,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            inputPlaceholder: "请输入价格",
            inputPattern:
              /^(([0])|([1-9]+[0-9]*.{1}[0-9]+)|([0].{1}[1-9]+[0-9]*)|([1-9][0-9]*)|([0][.][0-9]+[1-9]+))$/,
            inputErrorMessage: "价格格式不正确",
          }
        )
          .then(({ value }) => {
            that.setsell(parseInt(row[0]), value);
            this.$message({
              type: "success",
              message: "通证价格是: " + value,
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消输入",
            });
          });
      } else {
        this.$store.state.contractInstance1().unsell(
          row[0],
          {
            //gas: 300000,
            from: this.$store.state.web3.coinbase,
          },
          function (res) {
            console.log("取消selling!");
          }
        );
      }
    },
    async getNFT(nid, index) {
      let that = this;
      await this.$store.state.contractInstance().NFTS.call(
        nid, //用call返回一个结果，不使用call，返回的则是交易id
        {
          //gas: 300000,
          from: this.$store.state.web3.coinbase,
        },
        function (err, res) {
          that.nfts.push(res);
        }
      );
      // console.log(this.returnurl)
    },
    async getUserNft() {
      let that = this;
      await this.$store.state.contractInstance().findByAddress.call(
        {
          gas: 300000,
          from: this.$store.state.web3.coinbase,
        },
        function (err, res) {
          res.forEach((item, index) => {
            console.log("item = " + parseInt(item));
            that.getNFT("" + parseInt(item), index);
          });

          //console.log(res)
        }
      );
    },

    async setsell(mid, mprice) {
      let that = this;
      await this.$store.state.contractInstance1().sell(
        mid,
        mprice,
        {
          //gas: 300000,
          from: this.$store.state.web3.coinbase,
        },
        function (res) {
          console.log("setselling!");
        }
      );
    },
  },
};
</script>